﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Unis.Framework.Core.Utility;
using Unis.Framework.ORM.DbSql;
using Unis.Framework.ORM.Persistence;
using Unis.Framework.RuleBase.Attributes;
using Unis.Framework.RuleBase.Exceptions;
using Unis.RuleBase.Common;

namespace Unis.Framework.RuleBase.Common
{
    public class ObjectDataValidatedHelper
    {
        // Methods
        public bool CheckValueIsExists(object dataInDocType, object entity, string[] checkPropertys)
        {
            ObjectDataMappingAttribute objectDataMappingAttribute = AttributeConfigHelper.Instance.GetObjectDataMappingAttribute(dataInDocType);
            if (objectDataMappingAttribute == null)
            {
                throw new RequireConfigDataMappingException(dataInDocType);
            }
            string mappingTableName = objectDataMappingAttribute.MappingTableName;
            object obj2 = Reflection.GetObjectProperty(entity, objectDataMappingAttribute.KeyName);
            if (obj2 == null)
            {
                obj2 = string.Empty;
            }
            string query = string.Format("SELECT 1 FROM {0} WHERE ROWNUM <=1 AND {1}<>'{2}' AND ", mappingTableName, objectDataMappingAttribute.KeyName, obj2.ToString().Replace("'", "''"));
            string str3 = string.Empty;
            foreach (string str4 in checkPropertys)
            {
                if (!string.IsNullOrEmpty(str3))
                {
                    str3 = str3 + " AND ";
                }
                object obj3 = Reflection.GetObjectProperty(entity, str4);
                if (obj3 == null)
                {
                    obj3 = string.Empty;
                }
                str3 = str3 + string.Format("{0} = '{1}'", str4, obj3.ToString().Replace("'", "''"));
            }
            Database db = DatabaseHelper.CreateDatabase();
            query = query + str3;
            DbCommand sqlStringCommand = db.GetSqlStringCommand(query);
            object orgData = DatabaseExecuteHelper.NewInstance.ExecuteScalar(db, sqlStringCommand);
            return ((orgData != null) && Convert.ToBoolean(orgData));
        }

        public string[] RemoveNotExistsData(string tableName, string fieldName, string[] checkValues)
        {
            string format = "SELECT DISTINCT {0} FROM {1} WHERE {2} IN ({3})";
            string[] strArray = SqlShareHelper.Instance.SplitInSqlStringBySqlMaxLength<string>(checkValues);
            List<string> list = new List<string>();
            Database db = DatabaseHelper.CreateDatabase();
            DatabaseExecuteHelper newInstance = DatabaseExecuteHelper.NewInstance;
            foreach (string str2 in strArray)
            {
                string query = string.Format(format, new object[] { fieldName, tableName, fieldName, str2 });
                DbCommand sqlStringCommand = db.GetSqlStringCommand(query);
                IDataReader reader = newInstance.ExecuteReader(db, sqlStringCommand);
                try
                {
                    while (reader.Read())
                    {
                        if (reader[0] != DBNull.Value)
                        {
                            list.Add(reader[0].ToString());
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw new DatabaseExecuteException("在执行 RemoveNotExistsData 读取数据时出错", exception);
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                }
            }
            return list.ToArray();
        }

        public string[] RemoveNotExistsData(string xmlFileName, string sqlName, string fieldName, string[] checkValues, params object[] parValues)
        {
            string format = "{0} IN ({1})";
            string[] strArray = SqlShareHelper.Instance.SplitInSqlStringBySqlMaxLength<string>(checkValues);
            List<string> list = new List<string>();
            DatabaseExcuteByXmlHelper newInstance = DatabaseExcuteByXmlHelper.NewInstance;
            foreach (string str2 in strArray)
            {
                string item = string.Format(format, fieldName, str2);
                List<object> list2 = new List<object>();
                if ((parValues != null) && (parValues.Length > 0))
                {
                    foreach (object obj2 in parValues)
                    {
                        list2.Add(obj2);
                    }
                }
                list2.Add(item);
                DataSet set = newInstance.GetDataSetByXml(xmlFileName, sqlName, list2.ToArray());
                if (set.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in set.Tables[0].Select())
                    {
                        list.Add(row[0].ToString());
                    }
                }
            }
            return list.ToArray();
        }
    }
}
